import React from 'react'
import { Divider, Popover, Space, message } from 'antd'
import { awaitWrap, confirmAction, formatDate } from '@src/assets/js/tool'
import { delRequest } from './ajax'
import { addWindow } from '@src/redux/actions'
import style from './Index.module.less'

async function delHandle (id, callback) {
  const h = message.loading('删除中', 120)
  const [e] = await awaitWrap(delRequest(id))
  h()
  if (e === null) {
    message.success('删除成功')
    callback()
  }
}

// 获取表格字段配置
export function getColumns (dispatch, refreshFn) {
  return [
    {
      title: '序号',
      dataIndex: 'index',
      key: 'index',
      width: '60px',
      align: 'center'
    },
    {
      title: '上报年份',
      dataIndex: 'year',
      key: 'year',
      width: '80px'
    },
    {
      title: '上报人',
      dataIndex: 'operator',
      key: 'operator',
      width: '80px'
    },
    {
      title: '上报时间',
      dataIndex: 'create_datetime',
      key: 'create_datetime',
      width: '120px',
      render: (v) => {
        return formatDate(v, 'YYYY-MM-DD')
      }
    },
    {
      title: '上报结果',
      dataIndex: 'result_code',
      key: 'result_code',
      width: '100px',
      render: (v, l) => {
        if (l.result_message && v !== '0000') {
          return <Popover content={l.result_message}><span className={style.fail}>失败</span></Popover>
        }
        if (v === '0000') {
          return <span className={style.success}>成功</span>
        } else {
          return <span className={style.fail}>失败</span>
        }
      }
    },
    {
      title: '操作',
      dataIndex: 'result_code',
      key: 'result_code',
      width: '120px',
      render: (v, l) => {
        return (
          <Space split={<Divider type='vertical' />}>
            <a onClick={() => dispatch(addWindow({ id: `CaseAcceptSubmitDetail-${l.id}`, componentName: 'CaseAcceptSubmitDetail', title: '案件受理类型数据上报详情', meta: { id: l.id } }))}>详情</a>
            {v !== '0000' && <a onClick={() => confirmAction('您确定要删除吗？', () => { delHandle(l.id, refreshFn) })}>删除</a>}
          </Space>
        )
      }
    }
  ]
}
